<?php
	if(!comments_open() || 'open' != get_option('default_comment_status')) return;
?>

<div id="comments">

<?php
	$comments = get_comments(array(
		'post_id' => $id,
		'orderby' => 'comment_date',
		'order' => 'ASC',
		'type' => 'comment',
	));

	if($comments) : 
?>
	<div id="comment-list" class="card">
		<h2 class="card-header">用户评论</h2>
		<ol>

		<?php
			foreach($comments as $comment) {
		?>
			<li id="comment-<?php comment_ID(); ?>" class="media mt-3">
				<div class="mr-3">
					<?php echo get_avatar($comment, 46, '', '', array('class'=>'rounded-circle')); ?>
				</div>
				<div class="media-body">
					<div class="header">
						<h5 class="mt-1">
							<?php echo get_comment_author_link($comment); ?>
						</h5>
						<div class="metadata">
							<time datetime="<?php comment_time('c'); ?>">
								<?php echo get_comment_date(get_option('date_format') . ' ' . get_option('time_format'), $comment); ?>
							</time>
							<?php
								comment_reply_link( array(
									'add_below' => 'div-comment',
									'depth'     => -1,
									'max_depth' => 0,
									'before'    => '<span class="reply ml-1">',
									'after'     => '</span>'
								));
							?>
							<?php edit_comment_link('编辑', '<span class="edit-link ml-1">', '</span>'); ?>
						</div>
					</div>
					
					<div class="comment-content">
						<?php if(0 == $comment->comment_approved) : ?>
							<p class="comment-awaiting-moderation">您的评论等待审核中...</p>
						<?php endif; ?>
						
						<?php if($comment->comment_parent) : ?>
							<blockquote class="parent-comment">
								<p class="parent-author">
									<?php echo get_comment_author($comment->comment_parent) . ' 发表于 ' . get_comment_date(get_option('date_format') .' ' . get_option('time_format'), $comment->comment_parent); ?>
								</p>
								<p class="parent-content"><?php comment_excerpt($comment->comment_parent); ?></p>
							</blockquote>
						<?php endif; ?>
						<?php comment_text(); ?>
					</div>
				</div>
			</li>
		<?php } ?>
		</ol>
	</div>

<?php endif; ?>

	<div id="comment-respond" class="card">
		<?php 
			$commenter = wp_get_current_commenter();
			$req = get_option('require_name_email');
			$aria_req = $req ? ' aria-required="true"' : '';
			comment_form(array(
				'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title card-header">',
				'class_form' => 'form-row card-body',
				'title_reply' => '发表评论',
				'comment_notes_before' => '',
				'comment_notes_after' => '',
				'submit_button' => '<input type="submit" name="submit" class="btn" value="提交评论">',
				'submit_field' => '<div class="col-12 mt-2">%1$s %2$s</div>',
				'fields' => apply_filters('comment_form_default_fields', array(
					'author' => '<div class="col-12 col-md-6 mb-2"><input class="form-control" placeholder="昵称" id="author" name="author" type="text" value="' . $commenter['comment_author'] . '"' . $aria_req . '></div>',
					'email' => '<div class="col-12 col-md-6 mb-2"><input class="form-control" placeholder="邮箱" id="email" name="email" type="email" value="' .  $commenter['comment_author_email'] . '"' . $aria_req . '></div>',
					'url' => '<div class="col-12 mb-2"><input class="form-control" placeholder="网址" id="url" name="url" type="url" value="' .  $commenter['comment_author_url'] . '"></div>',
				)),
				'comment_field' =>  '<div class="col-12 mb-2 input-group-lg"><textarea class="form-control" rows="3" placeholder="我要吐槽..." id="comment" name="comment" aria-required="true"></textarea></div>',
			));
		?>
	</div>
</div>